package mapSystem;

import java.awt.Point;
import java.awt.image.BufferedImage;
import java.util.HashMap;

import tools.GameElement;

import mapSystem.interaction.TileAction;

public class GameMap {
	
	private Tile baseMap[][];
	private BufferedImage overMap[][];
	private TileAction actions[][];
	private HashMap<Integer, String> portals;
	private HashMap<Point, Point> portalsCoord;
	
	private MapHandler mapHandler;
	
	public GameMap(MapHandler handler)
	{
		baseMap = null;
		overMap = null;
		actions = null;
		portals = new HashMap<Integer, String>();
		portalsCoord = new HashMap<Point, Point>();
		
		mapHandler = handler;
	}
	
	public void setBaseMap(Tile map[][])
	{
		baseMap = map;
	}
	public Tile[][] getBaseMap()
	{
		return baseMap;
	}
	
	public void setOverMap(BufferedImage map[][])
	{
		overMap = map;
	}
	public BufferedImage[][] getOverMap()
	{
		return overMap;
	}

	public void addPortal(int id, String map)
	{
		portals.put(id, map);
	}
	public String getPortal(int id)
	{
		return portals.get(id);
	}
	public void setPortalCoord(int xp, int yp, int xt, int yt)
	{
		portalsCoord.put(new Point(xp, yp), new Point(xt, yt));
	}
	public Point getPortalCoord(int x, int y)
	{
		if(portalsCoord.containsKey(new Point(x, y))) return portalsCoord.get(new Point(x, y));
		
		System.out.println("Portal nao encontrado em ("+x+", "+y+")");
		return new Point(-1, -1);
	}
	
	public void setActions(TileAction acts[][])
	{
		actions = acts;
	}
	public void addAction(int x, int y, TileAction action)
	{
		actions[x][y] = action;
	}
	public TileAction[][] getActions()
	{
		return actions;
	}

	public GameElement autoActivate(int x, int y)
	{
		if(baseMap[x][y].getType() >= 2)
		{
			mapHandler.changeLevel(portals.get(baseMap[x][y].getType()), x, y);
		}else
		{
			if(actions != null)if(actions[x][y] != null) return actions[x][y].autoActivate();
		}
		return null;
	}
	public GameElement playerActivated(int x, int y)
	{
		if(actions != null)
		{
			if(x >= actions.length || y >= actions[0].length) return null;
			if(actions[x][y] != null) return actions[x][y].playerActivated();
		}
		return null;
	}

	public void clearMap()
	{
		baseMap = null;
		overMap = null;
		actions = null;
		portals = new HashMap<Integer, String>();
		portalsCoord = new HashMap<Point, Point>();
	}

	public boolean hasPortals() {
		return portals.size() > 0;
	}
}
